package view;

import entity.LoginUser;
import org.jb2011.lnf.beautyeye.ch3_button.BEButtonUI;
import services.lmpl.LoginUserServiceImpl;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;

public class LoginJFrame extends JFrame implements MouseListener, FocusListener {
    // 定义全局组件
    private JTextField username = new JTextField(20);
    private JPasswordField password = new JPasswordField(20);
    private JButton buttonLogin, buttonReset;

    public LoginJFrame() {
        Image backgrounImage = null;
        try {
            backgrounImage = ImageIO.read(new File("image/loginbackground.png"));
            Image imgae = ImageIO.read(new File("image/logo.png"));
            this.setIconImage(imgae);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 窗口背景面板
        ImagePanel backgroundPanel = new ImagePanel(backgrounImage);
        backgroundPanel.setLayout(null);

        username.setBounds(378, 202, 173, 30);
        username.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        username.addFocusListener(this);
        username.setText("用户名/账号");

        password.setBounds(378, 240, 173, 30);
        password.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        password.addFocusListener(this);
        password.setText("密码");
        password.setEchoChar('\0');

        buttonLogin = new JButton("登录");
        buttonLogin.setBounds(380, 280, 70, 27);
        buttonLogin.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.lightBlue));
        buttonLogin.setForeground(Color.white);
        buttonLogin.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        buttonLogin.addMouseListener(this);

        buttonReset = new JButton("重置");
        buttonReset.setBounds(480, 280, 70, 27);
        buttonReset.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.red));
        buttonReset.setForeground(Color.white);
        buttonReset.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        buttonReset.addMouseListener(this);

        backgroundPanel.add(username);
        backgroundPanel.add(password);
        backgroundPanel.add(buttonLogin);
        backgroundPanel.add(buttonReset);

        this.add(backgroundPanel);
        this.setTitle("  销售管理系统");
        this.setSize(830, 530);
        this.setVisible(true);
        this.requestFocus();
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
    }

    // 鼠标点击事件
    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getSource() == buttonLogin) {
            if ("用户名/账号".equals(username.getText())) {
                JOptionPane.showMessageDialog(null, "用户名不能为空");
            } else if ("密码".equals(password.getText())) {
                JOptionPane.showMessageDialog(null, "用户密码不能为空");
            } else {
                LoginUserServiceImpl loginUserService = new LoginUserServiceImpl();
                try {
                    LoginUser user = loginUserService.checkOne(username.getText(), password.getText());
                    if (user == null) {
                        JOptionPane.showMessageDialog(null, "用户名密码有误");
                    } else {
                        this.setVisible(false);
                        new IndexJFrame(user);
                    }
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        } else if (e.getSource() == buttonReset) {
            username.setText("用户名/账号");
            password.setText("密码");
            password.setEchoChar('\0');
        }
    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
    }

    @Override
    public void mouseExited(MouseEvent arg0) {
    }

    @Override
    public void mousePressed(MouseEvent arg0) {
    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
    }

    // 聚焦事件
    @Override
    public void focusGained(FocusEvent e) {
        if (e.getSource() == username) {
            if (username.getText().equals("用户名/账号")) {
                username.setText("");
            }
        } else if (e.getSource() == password) {
            if (password.getText().equals("密码")) {
                password.setText("");
                password.setEchoChar('*');
            }
        }
    }

    // 失焦事件
    @Override
    public void focusLost(FocusEvent e) {
        if (e.getSource() == username) {
            if (username.getText().equals("")) {
                username.setText("用户名/账号");
            }
        } else if (e.getSource() == password) {
            if (password.getText().equals("")) {
                password.setText("密码");
                password.setEchoChar('\0');
            }
        }
    }
}

